package com.example.demo.admin.gecco.crawler.mzt.image;

import com.example.demo.core.util.HttpClientUtil;
import com.geccocrawler.gecco.request.HttpGetRequest;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.request.StartRequestList;
import com.geccocrawler.gecco.scheduler.NoLoopStartScheduler;
import com.geccocrawler.gecco.scheduler.Scheduler;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Spider implements Runnable {

    public static int failedCount = 0;

//    private static  ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    private static Log log = LogFactory.getLog(Spider.class);
    private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1);
    private static String dounloadDir = "D:\\work\\img";
    private static String rootPage = "https://www.797ii.com";
    private static String indexPage = "https://www.797ii.com/htm/piclist1/1.htm";
    private static int num = 1;
    private static int maxNum = 1024;
    private static int curNum = 0;
    private Scheduler scheduler;

    private StringBuffer sb = new StringBuffer();


    public Spider() {
        scheduler = new NoLoopStartScheduler();


        StartRequestList start = new StartRequestList();
        start.setUrl(indexPage);

        scheduler.into(start.toRequest());

    }

    public static void main(String[] args) {

       HttpClientUtil.me().uploadFile("http://10.4.30.81:5688/cicada-example/upload","D:\\tmp\\FindBugs-IDEA-1.0.1.zip");

//        Spider spider = new Spider();
//        new Thread(spider).start();

//        try {
//            String json = Files.toString(new File("D:\\work\\img.txt")
//                    , Charset.forName("UTF-8"));
//
//            List<String> list = Splitter.on(";").splitToList(json);
//
//            RateLimiter limiter = RateLimiter.create(4.0);
//
//            for (String str : list) {
//                DownloadImage downloadImage = new DownloadImage(str, Spider.dounloadDir);
//
//
//                limiter.acquire();
//
//                Spider.fixedThreadPool.execute(downloadImage);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


    }

    @Override
    public void run() {

        String html;

        HttpRequest request = null;

        try {


            while ((request = scheduler.out()) != null) {

                if (curNum >= maxNum) break;

                Map<String, String> heads = new HashedMap();
                heads.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
                heads.put("Referrer Policy", "no-referrer-when-downgrade");
                heads.put("Remote Address","172.64.169.3:443");
                html = HttpClientUtil.me().httpGet(request.getUrl(), null);

                if(null !=html){

                    Document doc = Jsoup.parse(html);

                    menthdInvok(request.getUrl(), doc);
                }


                request = null;


            }
            File file = new File("D:\\work\\img.txt");

            Files.write(sb.toString(), file, Charsets.UTF_8);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void menthdInvok(String url, Document doc) {
        if (indexPage.equals(url)) {
            parsePage(doc);
        } else if (url.indexOf("list") >= 0) {
            parsePage(doc);
        } else {
            parseImg(doc);
        }
    }

    public void parsePage(Document doc) {
        Elements elements = doc.select(".mainArea li a");

        if (elements != null) {
            for (Element e : elements) {
                String imgSrc = e.attr("href");
                String text = e.text();
                int length = text.indexOf(" ");
                text = text.substring(0, length > 0 ? length : text.length());
                System.out.format("iamge src:{%s} text:{%s}", imgSrc, text);
                System.out.printf("\n");
                scheduler.into(new HttpGetRequest(rootPage + imgSrc));

            }

            if(elements.size()>0){

                num++;

                String nextUrl = indexPage.substring(0, indexPage.lastIndexOf("/") - 1) + num + ".htm";
                scheduler.into(new HttpGetRequest(nextUrl));
            }

        }

    }

    public void parseImg(Document doc) {

        Elements imgElements = doc.select(".picContent img");

        if (imgElements != null) {
            for (Element e : imgElements) {
                String imgSrc = e.attr("src");

                System.out.format("img src:{%s}", imgSrc);
                System.out.printf("\n");
                sb.append(imgSrc + ";");

                curNum++;

                log.info("当前数量:"+curNum);

            }

        }
    }

    public void strMatcher() {
        String pattern = "(?<=\\()(.+?)(?=\\))";

        String pa2 = "(?<=\\{)(.+?)(?=\\})";

        StringBuffer sb = new StringBuffer();

        try {
            String json = Files.toString(new File("D:\\work\\temp.txt")
                    , Charset.forName("UTF-8"));


            Pattern r = Pattern.compile(pa2);

            Matcher m = r.matcher(json);

            while (m.find()) {

                String value = m.group(0);

                if (value.indexOf("jpg") >= 0) {
                    sb.append(value + ";");
                }
            }

            System.out.printf(sb.toString());

            File file = new File("D:\\work\\img.txt");

            Files.write(sb.toString(), file, Charsets.UTF_8);


        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}
